package com.ckjw.gobang.view;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.ckjw.gobang.connect.UserClientService;
import com.ckjw.gobang.data.GameCenter;
import com.ckjw.gobang.data.Player;
import com.ckjw.gobang.data.Spot;
import com.ckjw.gobang.data.TableData;
import com.ckjw.gobang.utils.Path;
import com.ckjw.gobang.utils.randomtool;

/**
 * 控制游戏模式按钮
 * 
 * @author lck and djw
 * @Date 2022年6月10日-下午3:50:30
 */
public class ControlPanel extends JPanel {

	public static ControlPanel my;
	// 定义按钮
	JButton regame = new JButton("重新游戏");
	public static  JButton localgame = new JButton("本地对战");
	public static  JButton netgame = new JButton("网络对战");
	JButton clear = new JButton("清除记录");
	public static JTextArea jtext = new JTextArea("        游戏记录\n");
	JPanel record = new JPanel();
	// 创建容器容纳按钮
	Box box = Box.createVerticalBox();
	// 创建ScrollPane对象，并且指定默认有滚动条
	JScrollPane scrollPane = new JScrollPane(jtext);

	public ControlPanel() {
		my = this;
		init();
		listener();
	}

	/**
	 * 设置布局，组装按钮
	 */
	public void init() {
		my.add(record);
		my.add(box);
		// record
		record.setPreferredSize(new Dimension(180, 300));// 设置大小
		record.setBackground(Color.white);
		scrollPane.setPreferredSize(new Dimension(180, 240));// 设置大小
		jtext.setSelectedTextColor(Color.blue);
		jtext.setLineWrap(true);// 激活自动换行功能
		jtext.setWrapStyleWord(true); // 激活断行不断字功能
		jtext.setFont(new Font("仿宋", Font.BOLD + Font.ITALIC, 15));
		jtext.setForeground(Color.red);
		clear.setPreferredSize(new Dimension(180, 60));
		// 设置按钮图片
		clear.setIcon(new ImageIcon(Path.getsrcurl(this, "images/maininterface/delete2.png")));
		// clear.setBorderPainted(false);//不绘制边框
		record.add(scrollPane);
		record.add(clear);

		// box
		box.add(Box.createVerticalStrut(20));
		// regame.setPreferredSize(new Dimension(20,30));设置大小
		box.add(regame);
		box.add(Box.createVerticalStrut(20));
		box.add(localgame);
		box.add(Box.createVerticalStrut(20));
		box.add(netgame);
	}

	/**
	 * 添加监听器
	 */
	public void listener() {
		String text = "游戏还未结束，确定确定要重新游戏吗？";

		regame.addActionListener(event -> {
			if (!GameCenter.isEnd()) {
				int result = JOptionPane.showConfirmDialog(MainInterface.mainInterface, text, "确认对话框",
						JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (result == JOptionPane.YES_OPTION) {
					if (GameCenter.getMode()==GameCenter.MODE_ONLINE) {
						UserClientService.setover();
					}
					GameCenter.reStart();
					ChessBroad.myBroad2.repaint();
					jtext.append("游戏结果：强制退出\n");
					localgame.setEnabled(true);
					netgame.setEnabled(true);
				} else {

				}
			} else {
				GameCenter.reStart();
				ChessBroad.myBroad2.repaint();
				localgame.setEnabled(true);
				netgame.setEnabled(true);
			}
		});

		localgame.addActionListener(event -> {
			Boolean flag = true;
			UserPanel.userPanel.setVisible(true);
			UserPanel2.userPanel.setVisible(false);
			if (Player.myPlayer.getColor() == Spot.notChess) {
				String result = JOptionPane.showInputDialog(MainInterface.mainInterface, "请收入对战双方昵称，空格隔开", "",
						JOptionPane.INFORMATION_MESSAGE);
				if (result != null) {
					System.out.println(result);
				}
				
				result = result.trim();// 去掉前后空格
				if (!result.matches("[^ ]+[ ]+[^ ]+")) {// 只能是前面除空格字符中间空格，结尾除空格字符的形式
					System.out.println("输入格式有误");
					flag = false;// 因为不能return所以设置flag
				}
				// 设置用户面板的用户名，用户棋色
				if (flag) {
					String name[] = result.split(" +");
					System.out.println("玩家1：" + name[0]);
					System.out.println("玩家2：" + name[1]);
					// 设置用户信息
					setuser(Player.myPlayer, name[0], Spot.whiteChess, 0);
					setuser(Player.otherPlayer, name[1], Spot.blackChess, 1);
					UserPanel.userPanel.repaint();
					MainInterface.mainInterface.repaint();
				}
			}
				if (flag) {
				// 设置黑白棋哪个先行
				int tmp = randomtool.randomarea(0, 1);
				if (tmp == 1) {
					System.out.println("白棋先行");
					jtext.append("游戏开始：白棋先行\n");
					TableData.nowColor = Spot.whiteChess;
					JOptionPane.showMessageDialog(MainInterface.mainInterface, "白棋者先行", "提示消息",
							JOptionPane.WARNING_MESSAGE);
				} else {
					System.out.println("黒棋先行");
					jtext.append("游戏开始：黑棋先行\n");
					TableData.nowColor = Spot.blackChess;
					JOptionPane.showMessageDialog(MainInterface.mainInterface, "黑棋者先行", "提示消息",
							JOptionPane.WARNING_MESSAGE);
				}
			// 设置模式为双人模式
			GameCenter.setMode(GameCenter.MODE_LOCAL);

			// 发送开始信息到记录面板
			jtext.append("白方:-----" + Player.myPlayer.getName() + "\n");
			jtext.append("黑方:-----" + Player.otherPlayer.getName() + "\n");
			localgame.setEnabled(false);
			netgame.setEnabled(false);
		}
		});
		
		clear.addActionListener(event ->{
			jtext.setText("        游戏记录\n");
		});
		
		//网络对战
		netgame.addActionListener(event ->{
			if (Player.onlineuser==null) {
				JOptionPane.showMessageDialog(MainInterface.mainInterface, "检测到您还未登陆，请先登录", "提示消息",
						JOptionPane.WARNING_MESSAGE);
				try {
					new login().init();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else {
				UserPanel.userPanel.setVisible(false);
				UserPanel2.userPanel.setVisible(true);
				if (UserPanel2.selectUsertx==null) {
					JOptionPane.showMessageDialog(MainInterface.mainInterface, "请先在在线用户中选择一个进行对战", "提示消息",
							JOptionPane.WARNING_MESSAGE);
				}
				else {
					UserClientService.onlineGame();
				}
			}
		});
	}
	

	/**
	 * 初始化用户信息
	 * 
	 * @param player 用户对象
	 * @param name   要赋值的姓名 String
	 * @param clor   要赋给的棋色 String 详见spot类
	 * @param pos    左右位置 int 详见useranel类
	 */
	public void setuser(Player player, String name, String clor, int pos) {
		player.setName(name);
		player.start(clor);
		UserPanel.setUserInfo(player, pos);
	}
}
